<?php namespace Admin\Controller;
use Common\Model\Goods_list;
use Common\Model\Goods_attr;
//货品控制器
class GoodsListController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new Goods_list;
	}
	//货品列表
	public function index(){
		if(IS_POST){
//			p(Q('post.'));die;
			if(!$this->model->store()) View::error($this->model->getError());
			View::success("添加成功");
		}	
		//显示货品列表的表头和添加字段
		$gid=$_SESSION['gid'];
		$goods_attrModel=new Goods_attr;
		$titleData=$goods_attrModel->join('type_attr','shop_type_attr_taid','=','taid')
						->where("shop_goods_gid={$gid} AND class=1")
						->groupBy('taid')
						->get();
		foreach($titleData as $k=>$v){
			 $titleData[$k]['value']=$goods_attrModel->where("shop_type_attr_taid={$v['taid']} AND shop_goods_gid={$gid}")->get();
		}
//		p($titleData);
		View::with('titleData',$titleData);
		//显示货品列表的内容
		$data=$this->model->where("shop_goods_gid={$gid}")->get();
		foreach($data as $k=>$v){
			$data[$k]['combine']=explode('|', $v['combine']);
			foreach($data[$k]['combine'] as $key=>$value){
				$value=$goods_attrModel->where("gaid={$value}")->pluck('gavalue');
				$data[$k]['combine'][$key]=$value;
			}
		}
		View::with('data',$data);
	    View::make();
	}

	//编辑货品
	public function edit(){
		if(IS_POST){
			if(!$this->model->edit()) View::error("规格不能为空");
			View::success("修改成功",U('index'));		
		}
		$glid=Q('get.glid',0,'intval');
		//获取当前旧数据
		$oldData=$this->model->where("glid={$glid}")->find();
		$oldData['combine']=explode('|', $oldData['combine']);
		View::with('oldData',$oldData);
		//显示表头
		$gid=$oldData['shop_goods_gid'];
		$goods_attrModel=new Goods_attr;
		$titleData=$goods_attrModel->join('type_attr','shop_type_attr_taid','=','taid')
						->where("shop_goods_gid={$gid} AND class=1")
						->groupBy('taid')
						->get();
		foreach($titleData as $k=>$v){
			$titleData[$k]['value']=$goods_attrModel->where("shop_type_attr_taid={$v['taid']} AND shop_goods_gid={$gid}")->get();
		}
//		p($titleData);
		View::with('titleData',$titleData);
//		p($oldData);
	    View::make();
	}
	//删除货品
	public function del(){
		$glid=Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		View::success("删除成功"); 
	}
} 
 ?>